#!/usr/bin/env node

/**
 * 测试网络IP获取功能
 * 验证后端能否正确获取本机IP地址
 */

const { getNetworkIP, getNetworkURL, processImageURL } = require('./utils/getNetworkIP');

console.log('🧪 测试网络IP获取功能');
console.log('========================');

console.log('\n📡 获取本机网络IP:');
console.log('==================');
const networkIP = getNetworkIP();
console.log(`✅ 检测到的网络IP: ${networkIP}`);

console.log('\n🔗 测试URL生成:');
console.log('===============');
const testURL = getNetworkURL('3000', '/uploads/test.jpg');
console.log(`✅ 生成的完整URL: ${testURL}`);

console.log('\n🖼️ 测试图片URL处理:');
console.log('===================');

const testCases = [
  {
    name: 'localhost URL',
    input: 'http://localhost:3000/uploads/pcproductimg/pc_1757562130026_838738961.jpg',
    expected: `http://${networkIP}:3000/uploads/pcproductimg/pc_1757562130026_838738961.jpg`
  },
  {
    name: '127.0.0.1 URL',
    input: 'http://127.0.0.1:3000/uploads/pcproductimg/pc_1757562130026_838738961.jpg',
    expected: `http://${networkIP}:3000/uploads/pcproductimg/pc_1757562130026_838738961.jpg`
  },
  {
    name: '相对路径',
    input: '/uploads/pcproductimg/pc_1757562130026_838738961.jpg',
    expected: `http://${networkIP}:3000/uploads/pcproductimg/pc_1757562130026_838738961.jpg`
  },
  {
    name: '完整网络URL',
    input: 'https://picsum.photos/seed/service5/300/200',
    expected: 'https://picsum.photos/seed/service5/300/200'
  },
  {
    name: '空URL',
    input: '',
    expected: ''
  }
];

testCases.forEach((testCase, index) => {
  console.log(`\n${index + 1}. ${testCase.name}:`);
  console.log(`   输入: ${testCase.input}`);
  console.log(`   期望: ${testCase.expected}`);
  
  const result = processImageURL(testCase.input, '3000');
  const passed = result === testCase.expected;
  
  console.log(`   结果: ${result}`);
  console.log(`   状态: ${passed ? '✅ 通过' : '❌ 失败'}`);
});

console.log('\n🎯 后端修改总结:');
console.log('================');
console.log('1. ✅ 创建了 getNetworkIP.js 工具模块');
console.log('2. ✅ 修改了 cosmetic.js 中的商品图片URL处理');
console.log('3. ✅ 修改了 cosmetic.js 中的医院图片URL处理');
console.log('4. ✅ 自动检测本机网络IP，无需手动配置');

console.log('\n🚀 预期效果:');
console.log('============');
console.log('1. 🌐 后端自动获取本机网络IP地址');
console.log('2. 🔄 所有图片URL自动替换为网络IP');
console.log('3. 📱 真机测试时图片应该能正常加载');
console.log('4. 🔧 换网络时自动适配新的IP地址');

console.log('\n📋 测试步骤:');
console.log('============');
console.log('1. 重启后端服务');
console.log('2. 检查控制台是否显示 "🌐 自动获取网络IP: [IP地址]"');
console.log('3. 调用API接口，检查返回的图片URL是否使用网络IP');
console.log('4. 在真机上测试图片加载');

console.log('\n🎉 后端自动IP获取功能已实现！');
